<?php

namespace app\admin\controller\choi;


use app\common\controller\Backend;
use think\Cache;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Config extends Backend
{
    protected $configModel;

    protected $data = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->configModel = model('config');
        $this->data = [
            'title' => '',
            'title_en' => '',
            'content' => ''
        ];
    }

    /**
     * 大餐/嗨吃酵素饮
     */
    public function hcjsy()
    {
        $this->update($this->data, 'hcjsy', '嗨吃酵素饮');
        return $this->fetch();
    }

    /**
     * 小纤棒奇亚籽
     */
    public function xqbqyz()
    {
        $this->update($this->data, 'xqbqyz', '小纤棒奇亚籽');
        return $this->fetch();
    }

    /**
     * 小纤棒葡萄
     */
    public function xqbpt()
    {
        $this->update($this->data, 'xqbpt', '小纤棒葡萄');
        return $this->fetch();
    }

    /**
     * 薄荷酵素软糖
     */
    public function bhjsrt()
    {
        $this->update($this->data, 'bhjsrt', '薄荷酵素软糖');
        return $this->fetch();
    }

    /**
     * 小蜜管
     */
    public function xmg()
    {
        $this->update($this->data, 'xmg', '小蜜管');
        return $this->fetch();
    }

    /**
     * 品牌历程
     */
    public function abouthistory()
    {
        $this->update($this->data, 'about_history', '品牌历程');
        return $this->fetch();
    }

    /**
     * 品牌理念
     */
    public function aboutidea()
    {
        $this->update($this->data, 'about_idea', '品牌理念');
        return $this->fetch();
    }

    /**
     * 品牌介绍
     */
    public function aboutintro()
    {
        $this->update($this->data, 'about_intro', '品牌介绍');
        return $this->fetch();
    }

    /**
     * 品牌故事
     */
    public function aboutstory()
    {
        $this->update($this->data, 'about_story', '品牌故事');
        return $this->fetch();
    }

    /**
     * 吸油丸
     */
    public function oilpill()
    {
        $this->update($this->data, 'oilpill', '吸油丸');
        return $this->fetch();
    }

    /**
     * 清清梅
     */
    public function qinqinplum()
    {
        $this->update($this->data, 'qinqinplum', '清清梅');
        return $this->fetch();
    }

    /**
     * SOSO棒
     */
    public function sosostick()
    {
        $this->update($this->data, 'sosostick', 'SOSO棒');
        return $this->fetch();
    }

    /**
     * 夜间嗨吃
     */
    public function yjhc()
    {
        $this->update($this->data, 'yjhc', '夜间嗨吃');
        return $this->fetch();
    }

    /**
     * 合作机构
     */
    public function partners()
    {
        $this->update($this->data, 'partners', '合作机构');
        return $this->fetch();
    }

    /**
     * 专利成分
     */
    public function patent()
    {
        $this->update($this->data, 'patent', '专利成分');
        return $this->fetch();
    }

    /**
     * 健康知识
     */
    public function healthy()
    {
        $this->update($this->data, 'healthy', '健康知识');
        return $this->fetch();
    }

    /**
     * 郑多燕亲研故事
     */
    public function institutestory()
    {
        $this->update($this->data, 'institutestory', '郑多燕亲研故事');
        return $this->fetch();
    }

    /**
     * 联系方式
     */
    public function infomation()
    {
        $this->update($this->data, 'infomation', '联系方式');
        return $this->fetch();
    }



    /**
     * 站点配置
     */
    public function basic()
    {
        $data = [
            'name' => '',
            'title' => '',
            'keywords' => '',
            'description' => '',
            'email' => '',
            'mobile' => '',
            'address' => '',
            'beian' => ''
        ];
        $this->update($data, 'web_basic', '官网基础站点配置');
        return $this->view->fetch();
    }

    private function update($data, $name, $description = '')
    {
        $value = $this->value($name);
        $content = $this->param($value, $data);
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (!$params) {
                $this->error('参数不能为空');
            }
            $jsonParams = json_encode($params, JSON_UNESCAPED_UNICODE);
            if ($value) {
                if ($value != $jsonParams) {
                    $res = $this->configModel->where('name', $name)->update(['value' => $jsonParams]);
                } else {
                    $this->success('请求成功');
                }
            } else {
                $res = $this->configModel->save([
                    'name' => $name,
                    'tip' => $description,
                    'type' => 'array',
                    'content' => '',
                    'value' => $jsonParams
                ]);
            }
            //缓存
            Cache::set($name, $value, 0);
            $content = $params;
            $res ? $this->success('请求成功') : $this->error('请求失败');
        }
        $this->assign('content', $content);
    }

    private function param($value, $content)
    {
        return $value ? json_decode($value, true): $content;
    }

    private function value($name)
    {
        return $this->configModel->where('name', $name)->value('value');
    }

}
